﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_QuanXianBiaoService : It_QuanXianBiaoService
    {
        public readonly MyContext _Context;
        public t_QuanXianBiaoService(MyContext context)
        {
            _Context = context;
        }
        public t_QuanXianBiao Add(t_QuanXianBiao t_QuanXianBiao)
        {
            _Context.t_QuanXianBiao.Add(t_QuanXianBiao);
            _Context.SaveChanges();
            return _Context.t_QuanXianBiao.FirstOrDefault(x => x.BeiZhu1 == t_QuanXianBiao.BeiZhu1 && x.BeiZhu2 == t_QuanXianBiao.BeiZhu2 && x.BeiZhu3 == t_QuanXianBiao.BeiZhu3 && x.BeiZhu4 == t_QuanXianBiao.BeiZhu4 && x.BeiZhu5 == t_QuanXianBiao.BeiZhu5 && x.QuanXianMingCheng == t_QuanXianBiao.QuanXianMingCheng);

        }

        public bool Delete(int id)
        {
            _Context.t_QuanXianBiao.Remove(_Context.t_QuanXianBiao.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public t_QuanXianBiao GetById(int id)
        {
            return _Context.t_QuanXianBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_QuanXianBiao> GetList()
        {
            return _Context.t_QuanXianBiao.ToList();
        }
       
        public List<t_QuanXianBiao> GetList1()
        {
            return _Context.t_QuanXianBiao.Where("BeiZhu1==\"启用\"").ToList();
        }
        public List<t_QuanXianBiao> listshuju()
        {
            t_QuanXianBiao t_QuanXianBiao = new t_QuanXianBiao();
            return _Context.t_QuanXianBiao.Where(ReturnWhere(t_QuanXianBiao)).ToList();
        }
        public string YiFenPeiFenYe(t_QuanXianBiao t_QuanXianBiao, int start, int limit)
        {
            return Helper.ListToJsonAddFenYe(_Context.t_QuanXianBiao.Where(ReturnWhere(t_QuanXianBiao)).ToList(), start, limit);
        }
        public string ReturnWhere(t_QuanXianBiao t_QuanXianBiao)
        {
            string returnstr = "1=1";
            if (t_QuanXianBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_QuanXianBiao.BeiZhu1 + "\")";
            }
            if (t_QuanXianBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_QuanXianBiao.BeiZhu2 + "\")";
            }
            if (t_QuanXianBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_QuanXianBiao.BeiZhu3 + "\")";
            }
            if (t_QuanXianBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_QuanXianBiao.BeiZhu4 + "\")";
            }
            if (t_QuanXianBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_QuanXianBiao.BeiZhu5 + "\")";
            }

            if (t_QuanXianBiao.QuanXianMingCheng != null)
            {
                returnstr = returnstr + " and QuanXianMingCheng.Contains(\"" + t_QuanXianBiao.QuanXianMingCheng + "\")";
            }

            return returnstr;
        }

        public List<t_RenYuanQuanXian> QuanXian(int id)
        {
            return _Context.t_RenYuanQuanXian.Where(x => x.RenYuanId == id).ToList();
        }
    }
}
